package com.yuda.operationcenter.service.impl;

import java.util.List;

import com.yuda.common.core.utils.DateUtils;
import com.yuda.operationcenter.mapper.OperInformationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yuda.operationcenter.mapper.OperLabelMapper;
import com.yuda.operationcenter.domain.OperLabel;
import com.yuda.operationcenter.service.IOperLabelService;

/**
 * 标签配置Service业务层处理
 *
 * @author wyn
 * @date 2021-04-12
 */
@Service
public class OperLabelServiceImpl implements IOperLabelService {

    @Autowired
    private OperLabelMapper operLabelMapper;

    @Autowired
    private OperInformationMapper operInformationMapper;

    /**
     * 查询标签配置
     *
     * @param id 标签配置ID
     * @return 标签配置
     */
    @Override
    public OperLabel selectOperLabelById(Long id) {
        return operLabelMapper.selectOperLabelById(id);
    }

    /**
     * 查询标签配置列表
     *
     * @param operLabel 标签配置
     * @return 标签配置
     */
    @Override
    public List<OperLabel> selectOperLabelList(OperLabel operLabel) {
        return operLabelMapper.selectOperLabelList(operLabel);
    }

    /**
     * 新增标签配置
     *
     * @param operLabel 标签配置
     * @return 结果
     */
    @Override
    public int insertOperLabel(OperLabel operLabel) {

        boolean exist = operLabelMapper.existLabelName(operLabel.getLabelName());
        if (!exist) {
            operLabel.setCreateTime(DateUtils.getNowDate());
            return operLabelMapper.insertOperLabel(operLabel);
        }
        return -1;
    }

    /**
     * 修改标签配置
     *
     * @param operLabel 标签配置
     * @return 结果
     */
    @Override
    public int updateOperLabel(OperLabel operLabel) {

        //判断标签名称是不是和原来的一致
        OperLabel operLabel1 = operLabelMapper.selectOperLabelById(operLabel.getId());
        if (operLabel.getLabelName() != null && operLabel1.getLabelName() != null) {

            if (!operLabel1.getLabelName().equals(operLabel.getLabelName())) {
                boolean exist = operLabelMapper.existLabelName(operLabel.getLabelName());
                if (exist) {
                    return -1;
                }
            }
            //查询标签有没有被使用过
            if (operLabel.getStatus() == 0) {
                boolean b = operInformationMapper.checkIsUsed(new Long[]{operLabel.getId()});
                if(b) return -2;
            }
        }
        operLabel.setUpdateTime(DateUtils.getNowDate());
        return operLabelMapper.updateOperLabel(operLabel);
    }

    /**
     * 批量删除标签配置
     *
     * @param ids 需要删除的标签配置ID
     * @return 结果
     */
    @Override
    public int deleteOperLabelByIds(Long[] ids) {
        return operLabelMapper.deleteOperLabelByIds(ids);
    }

    /**
     * 删除标签配置信息
     *
     * @param id 标签配置ID
     * @return 结果
     */
    @Override
    public int deleteOperLabelById(Long id) {
        return operLabelMapper.deleteOperLabelById(id);
    }
}
